﻿using System;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace RibbonEditor.Controls
{
    public partial class StringControl : UserControl, IDirtable
    {
        bool firstAccess = true;

        string initialValue = null;

        public bool IsDirty
        {
            set { this.isDirty = value; if (value == false) this.initialValue = this.txtText.Text; this.isDirty = value; }
            get { return this.isDirty; }
        }

        private bool isDirty;

        private string format;

        public string Format
        {
            get { return format; }
            set { format = value; }
        }

        public string Label
        {
            get
            {
                return this.lblLabel.Text;
            }
            set
            {
                this.lblLabel.Text = value;
            }
        }

        public string Value
        {
            get
            {
                return this.txtText.Text;
            }
            set
            {
                if (initialValue == null)
                    initialValue = value;

                this.txtText.Text = value;
            }
        }

        public bool IsRequired
        {
            get
            {
                return this.lblRequired.Visible;
            }
            set
            {
                this.lblRequired.Visible = value;
            }
        }

        bool isEditable = true;

        public bool IsEditable
        {
            get { return isEditable; }
            set { isEditable = value; }
        }

        public StringControl()
        {
            InitializeComponent();

            this.txtText.Enabled = isEditable;
        }

        private void txtText_TextChanged(object sender, EventArgs e)
        {
            if (!firstAccess)
            {
                this.IsDirty = this.initialValue != this.txtText.Text;
            }
            else
            {
                firstAccess = false;
            }
        }


        public void ReinitValues()
        {
            this.txtText.Text = this.initialValue;
            this.isDirty = false;
        }
    }
}
